﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using static SRB.Tg.DrawingStandard.TextPloter;

namespace SRB.Tg.DrawingStandard
{
    public class RebarDimPloter
    {

        #region pile
        /// <summary>
        /// 绘制引线
        /// </summary>
        /// <param name="db"></param>
        /// <param name="point">引线起点坐标</param>
        /// <param name="T1">引线上部参数</param>
        /// <param name="T2">引线下部参数</param>
        /// <param name="T3">圆内参数</param>
        /// <param name="width">第二点距起点X轴距离</param>
        /// <param name="height">第二点距起点Y轴距离</param>
        /// <param name="Rotation">旋转角度</param>
        /// <param name="tstyle">样式</param>
        /// <param name="scale">比例</param>
        /// <returns></returns>
        public static void RebarWire(Database db, ref Extents2d ext, Point2d point, string T1, string T2, int T3,
           double Rotation, string tstyle = "SRB.HT", double scale = 100, double h= 15 )
        {
            tstyle = "SRB.HT";
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 第一条线
                Polyline Line = new Polyline() { Closed = false, LayerId = lt["H标注"] };
                Point2d point1 = point;
                Point2d point2 = point.C2D(2 * scale, 5 * scale);
                Point2d point3 = point2.C2D(h * scale);
                Line.AddVertexAt(0, point1, 0, 0, 0);
                Line.AddVertexAt(1, point2, 0, 0, 0);
                Line.AddVertexAt(2, point3, 0, 0, 0);

                ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                res.Add(Line);
                recorder.AppendEntity(Line);
                tr.AddNewlyCreatedDBObject(Line, true);
                var TX1 = Matrix3d.Rotation(GeTools.DegreeToRadian(Rotation), Vector3d.ZAxis, point.C3D());
                Line.TransformBy(TX1);
                // 圆内文字
                DBObjectCollection res3 = DimPloter.CircularMark(db, ref ext, point3.C3D(), T3.ToString(), tstyle, scale, GeTools.DegreeToRadian(Rotation));

                // top 文字
                Point2d pt = new Point2d(point2.X + 15 * scale / 2, point2.Y + scale*1.75);
                //DBObjectCollection res1 = TextPloter.PlotText(db,db, ref ext, TextPloter.eTxtLocation.E_TOP, pt, T1, scale, "SRB.tssdeng", GeTools.DegreeToRadian(Rotation));
                TextPloter.AddDBText(db,pt.C3D(), ref ext, T1, scale, 2.5, "SRB.tssdeng",0,TextHorizontalMode.TextMid,TextVerticalMode.TextBottom);
                  // bottom 文字
                  pt = new Point2d(point2.X + 15 * scale / 2, point2.Y - scale);
                DBObjectCollection res2 = TextPloter.PlotText(db, ref ext, TextPloter.eTxtLocation.E_BOTTOM, pt, T2, scale, tstyle, GeTools.DegreeToRadian(Rotation));


                //foreach (DBObject item in res)
                //{
                //    Entity pr = (Entity)item;
                //    pr.TransformBy(TX1);
                //}

                //foreach (DBObject item in res1)
                //{
                //    Entity pr = (Entity)item;
                //    pr.TransformBy(TX1);
                //}

                //foreach (DBObject item in res2)
                //{
                //    Entity pr = (Entity)item;
                //    pr.TransformBy(TX1);
                //}

                //foreach (DBObject item in res3)
                //{
                //    Entity pr = (Entity)item;
                //    pr.TransformBy(TX1);
                //}
                tr.Commit();
            }
        }

        public static void RebarWireNew(Database db, ref Extents2d ext, Point2d point, string T1, string T2, string T3,
      double Rotation, string tstyle = "SRB.HT", double scale = 100)
        {
            tstyle = "SRB.HT";
            DBObjectCollection res = new DBObjectCollection();
            double width1 = 0, height = 0;
            MText txt1 = TextPloter.GetTextActualWidth(db, T1, scale, out width1, out height, 2.5, Extensions.curFont);

            double width2 = 0;
            MText txt2 = TextPloter.GetTextActualWidth(db, T2, scale, out width2, out height, 2.5, Extensions.curFont);
            double len= width1> width2 ? width1: width2;
            len = len * 1.2;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 第一条线
                Polyline Line = new Polyline() { Closed = false, LayerId = lt["H标注"] };
                Point2d point1 = point;
                Point2d point2 = point.C2D(1.5 * scale, 5 * scale);
                Point2d point3 = point2.C2D(len);
                Line.AddVertexAt(0, point1, 0, 0, 0);
                Line.AddVertexAt(1, point2, 0, 0, 0);
                Line.AddVertexAt(2, point3, 0, 0, 0);

                ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                res.Add(Line);
                recorder.AppendEntity(Line);
                tr.AddNewlyCreatedDBObject(Line, true);
                var TX1 = Matrix3d.Rotation(GeTools.DegreeToRadian(Rotation), Vector3d.ZAxis, point.C3D());
                Line.TransformBy(TX1);
                // 圆内文字
                DBObjectCollection res3 = DimPloter.CircularMark(db, ref ext, point3.C3D(), T3.ToString(), tstyle, scale, GeTools.DegreeToRadian(Rotation));

                // top 文字
                Point2d pt = new Point2d(point2.X + len / 2, point2.Y + scale * 1.75);
                //DBObjectCollection res1 = TextPloter.PlotText(db,db, ref ext, TextPloter.eTxtLocation.E_TOP, pt, T1, scale, "SRB.tssdeng", GeTools.DegreeToRadian(Rotation));
                TextPloter.AddDBText(db,pt.C3D(), ref ext, T1, scale, 2.5, "SRB.tssdeng", 0, TextHorizontalMode.TextMid, TextVerticalMode.TextBottom);
                // bottom 文字
                pt = new Point2d(point2.X + len / 2, point2.Y - scale);
                DBObjectCollection res2 = TextPloter.PlotText(db, ref ext, TextPloter.eTxtLocation.E_BOTTOM, pt, T2, scale, tstyle, GeTools.DegreeToRadian(Rotation));


                tr.Commit();
            }
        }

        public static void RebarWireNew(Database db, ref Extents2d ext, Point2d point, Point2d point2, string T1, string T2, string T3,
double Rotation, string tstyle = "SRB.HT", double scale = 100)
        {
            tstyle = "SRB.HT";
            DBObjectCollection res = new DBObjectCollection();
            double width1 = 0, height = 0;
            MText txt1 = TextPloter.GetTextActualWidth(db, T1, scale, out width1, out height, 2.5, Extensions.curFont);

            double width2 = 0;
            MText txt2 = TextPloter.GetTextActualWidth(db, T2, scale, out width2, out height, 2.5, Extensions.curFont);
            double len = width1 > width2 ? width1 : width2;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 第一条线
                Polyline Line = new Polyline() { Closed = false, LayerId = lt["H标注"] };
                Point2d point1 = point;
                //Point2d point2 = point.C2D(1.5 * scale, 5 * scale);
                Point2d point3 = point2.C2D(len);
                Line.AddVertexAt(0, point1, 0, 0, 0);
                Line.AddVertexAt(1, point2, 0, 0, 0);
                Line.AddVertexAt(2, point3, 0, 0, 0);

                ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                res.Add(Line);
                recorder.AppendEntity(Line);
                tr.AddNewlyCreatedDBObject(Line, true);
                var TX1 = Matrix3d.Rotation(GeTools.DegreeToRadian(Rotation), Vector3d.ZAxis, point.C3D());
                Line.TransformBy(TX1);
                // 圆内文字
                DBObjectCollection res3 = DimPloter.CircularMark(db, ref ext, point3.C3D(), T3.ToString(), tstyle, scale, GeTools.DegreeToRadian(Rotation));

                // top 文字
                Point2d pt = new Point2d(point2.X + len / 2, point2.Y + scale * 1.75);
                //DBObjectCollection res1 = TextPloter.PlotText(db,db, ref ext, TextPloter.eTxtLocation.E_TOP, pt, T1, scale, "SRB.tssdeng", GeTools.DegreeToRadian(Rotation));
                TextPloter.AddDBText(db,pt.C3D(), ref ext, T1, scale, 2.5, "SRB.tssdeng", 0, TextHorizontalMode.TextMid, TextVerticalMode.TextBottom);
                // bottom 文字
                pt = new Point2d(point2.X + len / 2, point2.Y - scale);
                DBObjectCollection res2 = TextPloter.PlotText(db, ref ext, TextPloter.eTxtLocation.E_BOTTOM, pt, T2, scale, tstyle, GeTools.DegreeToRadian(Rotation));


                tr.Commit();
            }
        }

        public static void RebarWire650(Database db, ref Extents2d ext, Point2d point, Point2d point2, string T1, string T2, string T3, string D,
double Rotation, string tstyle = "SRB.HT", double scale = 100)
        {
            tstyle = "SRB.HT";
            DBObjectCollection res = new DBObjectCollection();
            double width1 = 0, height = 0;
            MText txt1 = TextPloter.GetTextActualWidth(db, T1, scale, out width1, out height, 2.5, Extensions.curFont);

            double width2 = 0;
            MText txt2 = TextPloter.GetTextActualWidth(db, T2, scale, out width2, out height, 2.5, Extensions.curFont);
    
            double len = width1 > width2 ? width1 + 1 * scale : width2 + 1 * scale;
            Point2d ptD;
            if (width1 > width2)
               ptD = new Point2d(point2.X + (len) / 2 + 1.5 * scale, point2.Y + scale * 1.75);
            else
                ptD = new Point2d(point2.X + len / 2, point2.Y + scale * 1.75);
            string path = Extensions.curAppPath + "HRB650E.dwg";
            //string _blcName = "650"; 
                  string _blcName = "HRB650E";
            BlockPlot.InsertBlockByDwg(db, path, _blcName, "H钢筋", ptD.C3D(), ref ext, scale*0.85, 0, true, false);
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 第一条线
                Polyline Line = new Polyline() { Closed = false, LayerId = lt["H标注"] };
                Point2d point1 = point;
                //Point2d point2 = point.C2D(1.5 * scale, 5 * scale);
                Point2d point3 = point2.C2D(len);
                Line.AddVertexAt(0, point1, 0, 0, 0);
                Line.AddVertexAt(1, point2, 0, 0, 0);
                Line.AddVertexAt(2, point3, 0, 0, 0);

                ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                res.Add(Line);
                recorder.AppendEntity(Line);
                tr.AddNewlyCreatedDBObject(Line, true);
                var TX1 = Matrix3d.Rotation(GeTools.DegreeToRadian(Rotation), Vector3d.ZAxis, point.C3D());
                Line.TransformBy(TX1);
                // 圆内文字
                DBObjectCollection res3 = DimPloter.CircularMark(db, ref ext, point3.C3D(), T3.ToString(), tstyle, scale, GeTools.DegreeToRadian(Rotation));

                // top 文字
                Point2d pt = new Point2d(point2.X + len / 2, point2.Y + scale * 1.75);
                //DBObjectCollection res1 = TextPloter.PlotText(db,db, ref ext, TextPloter.eTxtLocation.E_TOP, pt, T1, scale, "SRB.tssdeng", GeTools.DegreeToRadian(Rotation));
                //TextPloter.AddDBText(db,pt.C3D(), ref ext, T1, scale, 2.5, "SRB.tssdeng", 0, TextHorizontalMode.TextMid, TextVerticalMode.TextBottom);
              
                TextPloter.AddDBText(db,pt.C3D(), ref ext, T1.Replace(D,"  "), scale, 2.5, "SRB.tssdeng", 0, TextHorizontalMode.TextMid, TextVerticalMode.TextBottom);
                ptD = pt;
                             // bottom 文字
                pt = new Point2d(point2.X + len / 2, point2.Y - scale);
                DBObjectCollection res2 = TextPloter.PlotText(db, ref ext, TextPloter.eTxtLocation.E_BOTTOM, pt, T2, scale, tstyle, GeTools.DegreeToRadian(Rotation));


                tr.Commit();
            }


        }

        public static void RebarWire650New(Database db, ref Extents2d ext, Point2d point, Point2d point2, string T1, string T2, string T3, string D,
double Rotation, string tstyle = "SRB.HT", double scale = 100)
        {
            tstyle = "SRB.HT";
            DBObjectCollection res = new DBObjectCollection();
            double width1 = 0, height = 0;
            MText txt1 = TextPloter.GetTextActualWidth(db, T1, scale, out width1, out height, 2.5, Extensions.curFont);

            double width2 = 0;
            MText txt2 = TextPloter.GetTextActualWidth(db, T2, scale, out width2, out height, 2.5, Extensions.curFont);

            double len = width1 > width2 ? width1+1*scale : width2 + 1 * scale;
            Point2d ptD = new Point2d(point2.X + len / 2+1.25*scale, point2.Y + scale * 1.75);
            string path = Extensions.curAppPath + "HRB650E.dwg";
            //string _blcName = "650";
            string _blcName = "HRB650E";
            BlockPlot.InsertBlockByDwg(db, path, _blcName, "H钢筋", ptD.C3D(), ref ext, scale, 0, true, false);
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 第一条线
                Polyline Line = new Polyline() { Closed = false, LayerId = lt["H标注"] };
                Point2d point1 = point;
                //Point2d point2 = point.C2D(1.5 * scale, 5 * scale);
                Point2d point3 = point2.C2D(len);
                Line.AddVertexAt(0, point1, 0, 0, 0);
                Line.AddVertexAt(1, point2, 0, 0, 0);
                Line.AddVertexAt(2, point3, 0, 0, 0);

                ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                res.Add(Line);
                recorder.AppendEntity(Line);
                tr.AddNewlyCreatedDBObject(Line, true);
                var TX1 = Matrix3d.Rotation(GeTools.DegreeToRadian(Rotation), Vector3d.ZAxis, point.C3D());
                Line.TransformBy(TX1);
                // 圆内文字
                DBObjectCollection res3 = DimPloter.CircularMark(db, ref ext, point3.C3D(), T3.ToString(), tstyle, scale, GeTools.DegreeToRadian(Rotation));

                // top 文字
                Point2d pt = new Point2d(point2.X + len / 2, point2.Y + scale * 1.75);
                //DBObjectCollection res1 = TextPloter.PlotText(db,db, ref ext, TextPloter.eTxtLocation.E_TOP, pt, T1, scale, "SRB.tssdeng", GeTools.DegreeToRadian(Rotation));
                //TextPloter.AddDBText(db,pt.C3D(), ref ext, T1, scale, 2.5, "SRB.tssdeng", 0, TextHorizontalMode.TextMid, TextVerticalMode.TextBottom);

                TextPloter.AddDBText(db,pt.C3D(), ref ext, T1.Replace(D, "  "), scale, 2.5, "SRB.tssdeng", 0, TextHorizontalMode.TextMid, TextVerticalMode.TextBottom);
                ptD = pt;
                // bottom 文字
                pt = new Point2d(point2.X + len / 2, point2.Y - scale);
                DBObjectCollection res2 = TextPloter.PlotText(db, ref ext, TextPloter.eTxtLocation.E_BOTTOM, pt, T2, scale, tstyle, GeTools.DegreeToRadian(Rotation));


                tr.Commit();
            }


        }
        /// <summary>
        /// 添加引线
        /// </summary>
        /// <param name="db"></param>
        /// <param name="point"></param>
        /// <param name="T1"></param>
        /// <param name="T2"></param>
        /// <param name="T3"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="Rotation"></param>
        /// <param name="tstyle"></param>
        /// <param name="scale"></param>
        /// <returns></returns>
        public static void CreateLeadWire(Database db, ref Extents2d ext, Point3d point, string T1, string T2, int T3,
        string tstyle = "SRB.HT", double scale = 100)
        {
            tstyle = "SRB.HT";
            double cR = 2.5;
            DBText txt = new DBText();
            Circle C2 = new Circle();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                // 第一条线
                Polyline Line = new Polyline() { Closed = false, LayerId = lt["H标注"] };
                Point2d point1 = point.C2D();
                Point2d point2 = point.C2D(5 * scale, 5 * scale);
                Point2d point3 = point2.C2D(15 * scale);
                Line.AddVertexAt(0, point1, 0, 0, 0);
                Line.AddVertexAt(1, point2, 0, 0, 0);
                Line.AddVertexAt(2, point3, 0, 0, 0);
                recorder.AppendEntity(Line);
                tr.AddNewlyCreatedDBObject(Line, true);
                ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));

                txt.TextString = T3.ToString();
                txt.Height = 2.5 * scale;
                txt.Position = point3.C3D(cR * scale);
                txt.HorizontalMode = TextHorizontalMode.TextCenter;
                txt.VerticalMode = TextVerticalMode.TextVerticalMid;
                txt.AlignmentPoint = point3.C3D(cR * scale);
                txt.TextStyleId = st[Extensions.curFont];
                txt.LayerId = lt["H标注"];
                txt.WidthFactor = 0.8;

                C2 = new Circle(point3.C3D(cR * scale), Vector3d.ZAxis, cR * scale);
                C2.LayerId = lt["H标注"];
                recorder.AppendEntity(txt);
                tr.AddNewlyCreatedDBObject(txt, true);
                recorder.AppendEntity(C2);
                tr.AddNewlyCreatedDBObject(C2, true);
                ext = ext.Add(new Extents2d(txt.Bounds.Value.MinPoint.C2D(), txt.Bounds.Value.MaxPoint.C2D()));
                ext = ext.Add(new Extents2d(C2.Bounds.Value.MinPoint.C2D(), C2.Bounds.Value.MaxPoint.C2D()));

                // top 文字
                Point2d pt = new Point2d(point2.X + 15 * scale / 2, point2.Y);
                TextPloter.PlotText(db, ref ext, TextPloter.eTxtLocation.E_TOP, pt, T1, scale, tstyle, GeTools.DegreeToRadian(0));

                // bottom 文字
                pt = new Point2d(point2.X + 15 * scale / 2, point2.Y);
                TextPloter.PlotText(db, ref ext, TextPloter.eTxtLocation.E_BOTTOM, pt, T2, scale, tstyle, GeTools.DegreeToRadian(0));
                tr.Commit();
            }

        }

        /// <summary>
        /// 不带圆形标记的引线
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="point"></param>
        /// <param name="T1"></param>
        /// <param name="T2"></param>
        /// <param name="tstyle"></param>
        /// <param name="scale"></param>
        public static void CreateLeadWireWithoutCircleMark(Database db, ref Extents2d ext, Point3d point, string T1, string T2,
        string tstyle = "SRB.HT", double scale = 100,double h=0, double w = 0)
        {
            tstyle = "SRB.HT";
            double cR = 2.5;
            DBText txt = new DBText();
            Circle C2 = new Circle();
            // 第一条线
            double width1 = 0, height = 0;
            MText txt1 = TextPloter.GetTextActualWidth(db, T1, scale, out width1, out height, 2.5, Extensions.curFont);

            double width2 = 0;
            MText txt2 = TextPloter.GetTextActualWidth(db, T2, scale, out width2, out height, 2.5, Extensions.curFont);
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

  

                double lineWidth = width1 > width2 ? width1 : width2;
                Polyline Line = new Polyline() { Closed = false, LayerId = lt["H标注"] };
                Point2d point1 = point.C2D();
                if (h == 0)
                    h = lineWidth / 2;
                if (w == 0)
                    w = lineWidth / 3;
                Point2d point2 = point.C2D(w, h);
                Point2d point3 = point2.C2D(lineWidth*1.1);
                Line.AddVertexAt(0, point1, 0, 0, 0);
                Line.AddVertexAt(1, point2, 0, 0, 0);
                Line.AddVertexAt(2, point3, 0, 0, 0);

                ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));

                // top 文字
                Point2d pt = new Point2d(point2.X + lineWidth / 2, point2.Y+ scale * 0.5);
                TextPloter.PlotText(db, ref ext, TextPloter.eTxtLocation.E_TOP, pt, T1, scale, tstyle, GeTools.DegreeToRadian(0));

                // bottom 文字
                pt = new Point2d(point2.X + lineWidth / 2+1*scale, point2.Y - scale * 0.75);
                TextPloter.PlotText(db, ref ext, TextPloter.eTxtLocation.E_BOTTOM, pt, T2, scale, tstyle, GeTools.DegreeToRadian(0));
                recorder.AppendEntity(Line);
                tr.AddNewlyCreatedDBObject(Line, true);
                tr.Commit();
            }

        }


        /// <summary>
        /// 钩线标记
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="startPoint"></param>
        /// <param name="T1"></param>
        /// <param name="lineLength"></param>
        /// <param name="scale"></param>
        public static void CreateHookLineMark(Database db, ref Extents2d ext, Point3d startPoint,
            string T1, double lineLength, double scale, double rotation = 0)
        {
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 线
                double valueX = 1 * scale;
                Point3d pt1 = startPoint.C3D(valueX, valueX);
                Polyline pl1 = new Polyline() { Closed = false, LayerId = lt["H标注"] };
                pl1.AddVertexAt(0, startPoint.C2D(lineLength), 0, 0, 0);
                pl1.AddVertexAt(1, startPoint.C2D(), 0, 0, 0);
                //pl1.AddVertexAt(2, startPoint.C2D(valueX, valueX * 0.5), 0, 0, 0);
                ext = ext.Add(new Extents2d(pl1.Bounds.Value.MinPoint.C2D(), pl1.Bounds.Value.MaxPoint.C2D()));
                res.Add(pl1);

                // 箭头
                Point2d pt3 = startPoint.C2D(0);
                Point2d pt4 = startPoint.C2D(valueX*1.25, -valueX * 0.25);
                Point2d pt5 = startPoint.C2D(valueX * 1.25, valueX * 0.25);

                Solid s1 = new Solid(pt3.C3D(), pt4.C3D(), pt5.C3D()) { LayerId = lt["H标注"] };
                res.Add(s1);

                // 圆形标记
                //DBObjectCollection res1 = DimPloter.CircularMark(db, ref ext, startPoint.C3D(lineLength), T1, Extensions.curFont, scale, GeTools.DegreeToRadian(360 - rotation));
                double cr = 2.5;
                // 圆内文字
                DBText Mtext = new DBText() { LayerId = lt["H标注"]};
                Mtext.Height = 2.5 * scale;
                Mtext.TextString = T1.ToString();
                Mtext.TextStyleId = st[Extensions.curFont];
                Mtext.WidthFactor = 0.8;
                Mtext.Rotation = GeTools.DegreeToRadian(360 - rotation);
                cr = Mtext.Height;
                Mtext.HorizontalMode = TextHorizontalMode.TextCenter;
                Mtext.VerticalMode = TextVerticalMode.TextVerticalMid;
                Mtext.AlignmentPoint = startPoint.C3D(lineLength).C3D(cr);
                Mtext.Position = startPoint.C3D(lineLength).C3D(cr);
                ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.C2D(), Mtext.Bounds.Value.MaxPoint.C2D()));
                res.Add(Mtext);

                recorder.AppendEntity((Entity)Mtext);
                tr.AddNewlyCreatedDBObject(Mtext, true);

                // 圆
                Circle a = new Circle(startPoint.C3D(lineLength).C3D(cr), Vector3d.ZAxis, cr);
                a.LayerId = lt["H标注"];
                ext = ext.Add(new Extents2d(a.Bounds.Value.MinPoint.C2D(), a.Bounds.Value.MaxPoint.C2D()));
                res.Add(a);
                recorder.AppendEntity((Entity)a);
                tr.AddNewlyCreatedDBObject(a, true);
                // 旋转
                var TX1 = Matrix3d.Rotation(GeTools.DegreeToRadian(rotation), Vector3d.ZAxis, startPoint.C3D());
                foreach (DBObject item in res)
                {
                    Entity pr = (Entity)item;
                    pr.TransformBy(TX1);
                }
                //foreach (DBObject item in res1)
                //{
                //    Entity pr = (Entity)item;
                //    pr.TransformBy(TX1);
                //}

                foreach (DBObject drawitem in res)
                {
                    recorder.AppendEntity((Entity)drawitem);
                    tr.AddNewlyCreatedDBObject(drawitem, true);
                }

                tr.Commit();
            }
        }

        public static void CreateHookLineMark(Database db, ref Extents2d ext, Point3d startPoint,List<Point3d> lstP,
      string T1, double lineLength, double scale, double rotation = 0)
        {
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 线
                double valueX = 1 * scale;
                //Point3d pt1 = startPoint.C3D(valueX, valueX);
                //Polyline pl1 = new Polyline() { Closed = false, LayerId = lt["H标注"] };
                //pl1.AddVertexAt(0, startPoint.C2D(lineLength), 0, 0, 0);
                //pl1.AddVertexAt(1, startPoint.C2D(), 0, 0, 0);
                ////pl1.AddVertexAt(2, startPoint.C2D(valueX, valueX * 0.5), 0, 0, 0);
                //ext = ext.Add(new Extents2d(pl1.Bounds.Value.MinPoint.C2D(), pl1.Bounds.Value.MaxPoint.C2D()));
                //res.Add(pl1);
              
             

                // 箭头
                //Point2d pt3 = startPoint.C2D(0);
                //Point2d pt4 = startPoint.C2D(valueX * 1.25, -valueX * 0.25);
                //Point2d pt5 = startPoint.C2D(valueX * 1.25, valueX * 0.25);

                //Solid s1 = new Solid(pt3.C3D(), pt4.C3D(), pt5.C3D()) { LayerId = lt["H标注"] };
                //res.Add(s1);

                // 圆形标记
                //DBObjectCollection res1 = DimPloter.CircularMark(db, ref ext, startPoint.C3D(lineLength), T1, Extensions.curFont, scale, GeTools.DegreeToRadian(360 - rotation));
                double cr = 2.5;
                // 圆内文字
                DBText Mtext = new DBText() { LayerId = lt["H标注"] };
                Mtext.Height = 2.5 * scale;
                Mtext.TextString = T1.ToString();
                Mtext.TextStyleId = st[Extensions.curFont];
                Mtext.WidthFactor = 0.8;
                Mtext.Rotation = GeTools.DegreeToRadian(360 - rotation);
                cr = Mtext.Height;
                Mtext.HorizontalMode = TextHorizontalMode.TextCenter;
                Mtext.VerticalMode = TextVerticalMode.TextVerticalMid;
                Mtext.AlignmentPoint = startPoint.C3D(lineLength).C3D(cr);
                Mtext.Position = startPoint.C3D(lineLength).C3D(cr);
                ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.C2D(), Mtext.Bounds.Value.MaxPoint.C2D()));
                res.Add(Mtext);

                recorder.AppendEntity((Entity)Mtext);
                tr.AddNewlyCreatedDBObject(Mtext, true);

                // 圆
                Circle a = new Circle(startPoint.C3D(lineLength).C3D(cr), Vector3d.ZAxis, cr);
                a.LayerId = lt["H标注"];
                ext = ext.Add(new Extents2d(a.Bounds.Value.MinPoint.C2D(), a.Bounds.Value.MaxPoint.C2D()));
                res.Add(a);
                recorder.AppendEntity((Entity)a);
                tr.AddNewlyCreatedDBObject(a, true);
                // 旋转
                //var TX1 = Matrix3d.Rotation(GeTools.DegreeToRadian(rotation), Vector3d.ZAxis, startPoint.C3D());
                //foreach (DBObject item in res)
                //{
                //    Entity pr = (Entity)item;
                //    pr.TransformBy(TX1);
                //}
                for (int i = 0; i < lstP.Count; i++)
                {
                    Polyline pl1 = new Polyline() { Closed = false, LayerId = lt["H标注"] };
                    pl1.AddVertexAt(0, startPoint.C2D(lineLength), 0, 0, 0);
                    pl1.AddVertexAt(1, lstP[i].C2D(), 0, 0, 0);
                    //pl1.AddVertexAt(2, startPoint.C2D(valueX, valueX * 0.5), 0, 0, 0);
                    ext = ext.Add(new Extents2d(pl1.Bounds.Value.MinPoint.C2D(), pl1.Bounds.Value.MaxPoint.C2D()));
                    res.Add(pl1);
                }

                foreach (DBObject drawitem in res)
                {
                    recorder.AppendEntity((Entity)drawitem);
                    tr.AddNewlyCreatedDBObject(drawitem, true);
                }

                tr.Commit();
            }
        }


        public static void CreateHookLineMarkNew(Database db, ref Extents2d ext, Point3d startPoint,
      string T1, double lineLength, double scale, double rotation = 0)
        {
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 线
                double valueX = 1 * scale;
                Point3d pt1 = startPoint.C3D(valueX, valueX);
                Polyline pl1 = new Polyline() { Closed = false, LayerId = lt["H标注"] };
                pl1.AddVertexAt(0, startPoint.C2D(lineLength), 0, 0, 0);
                pl1.AddVertexAt(1, startPoint.C2D(), 0, 0, 0);
                pl1.AddVertexAt(2, startPoint.C2D(valueX*0.5, valueX), 0, 0, 0);
                ext = ext.Add(new Extents2d(pl1.Bounds.Value.MinPoint.C2D(), pl1.Bounds.Value.MaxPoint.C2D()));
                res.Add(pl1);

                // 圆形标记
                //DBObjectCollection res1 = DimPloter.CircularMark(db, ref ext, startPoint.C3D(lineLength), T1, Extensions.curFont, scale, GeTools.DegreeToRadian(360 - rotation));
                double cr = 2.5;
                // 圆内文字
                DBText Mtext = new DBText() { LayerId = lt["H标注"] };
                Mtext.Height = 2.5 * scale;
                Mtext.TextString = T1.ToString();
                Mtext.TextStyleId = st[Extensions.curFont];
                Mtext.WidthFactor = 0.8;
                Mtext.Rotation = GeTools.DegreeToRadian(360 - rotation);
                cr = Mtext.Height;
                Mtext.HorizontalMode = TextHorizontalMode.TextCenter;
                Mtext.VerticalMode = TextVerticalMode.TextVerticalMid;
                Mtext.AlignmentPoint = startPoint.C3D(lineLength).C3D(cr);
                Mtext.Position = startPoint.C3D(lineLength).C3D(cr);
                ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.C2D(), Mtext.Bounds.Value.MaxPoint.C2D()));
                res.Add(Mtext);

                recorder.AppendEntity((Entity)Mtext);
                tr.AddNewlyCreatedDBObject(Mtext, true);

                // 圆
                Circle a = new Circle(startPoint.C3D(lineLength).C3D(cr), Vector3d.ZAxis, cr);
                a.LayerId = lt["H标注"];
                ext = ext.Add(new Extents2d(a.Bounds.Value.MinPoint.C2D(), a.Bounds.Value.MaxPoint.C2D()));
                res.Add(a);
                recorder.AppendEntity((Entity)a);
                tr.AddNewlyCreatedDBObject(a, true);
                // 旋转
                var TX1 = Matrix3d.Rotation(GeTools.DegreeToRadian(rotation), Vector3d.ZAxis, startPoint.C3D());
                foreach (DBObject item in res)
                {
                    Entity pr = (Entity)item;
                    pr.TransformBy(TX1);
                }
                //foreach (DBObject item in res1)
                //{
                //    Entity pr = (Entity)item;
                //    pr.TransformBy(TX1);
                //}

                foreach (DBObject drawitem in res)
                {
                    recorder.AppendEntity((Entity)drawitem);
                    tr.AddNewlyCreatedDBObject(drawitem, true);
                }

                tr.Commit();
            }
        }


        public static void CreateNewHookLineMark(Database db, ref Extents2d ext, Point3d startPoint, List<Point3d> lstP,
   string T1, double scale, bool isV = true,bool isAdd=false)
        {
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 线
                double valueX = 1 * scale;
                double cr = 2.5*scale;

                // 圆
                Circle a = new Circle(startPoint.C3D(cr), Vector3d.ZAxis, cr);
                if (!isV)
                {
                    if (isAdd)
                        a = new Circle(startPoint.C3D(0, cr), Vector3d.ZAxis, cr);
                    else
                        a = new Circle(startPoint.C3D(0, -cr), Vector3d.ZAxis, cr);
                }
                else
                {
                    if (isAdd)
                        a = new Circle(startPoint.C3D(cr), Vector3d.ZAxis, cr);
                    else
                        a = new Circle(startPoint.C3D(-cr), Vector3d.ZAxis, cr);
                }
                a.LayerId = lt["H标注"];
                ext = ext.Add(new Extents2d(a.Bounds.Value.MinPoint.C2D(), a.Bounds.Value.MaxPoint.C2D()));
                res.Add(a);
                recorder.AppendEntity((Entity)a);
                tr.AddNewlyCreatedDBObject(a, true);
                // 圆内文字
                DBText Mtext = new DBText() { LayerId = lt["H标注"] };
                Mtext.Height = 2.5 * scale;
                Mtext.TextString = T1.ToString();
                Mtext.TextStyleId = st[Extensions.curFont];
                Mtext.WidthFactor = 0.8;
                //Mtext.Rotation = GeTools.DegreeToRadian(360 - rotation);
                cr = Mtext.Height;
                Mtext.HorizontalMode = TextHorizontalMode.TextCenter;
                Mtext.VerticalMode = TextVerticalMode.TextVerticalMid;
                Mtext.AlignmentPoint = a.Center;
                Mtext.Position = a.Center;
                ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.C2D(), Mtext.Bounds.Value.MaxPoint.C2D()));
                res.Add(Mtext);

                recorder.AppendEntity((Entity)Mtext);
                tr.AddNewlyCreatedDBObject(Mtext, true);

             
              
                for (int i = 0; i < lstP.Count; i++)
                {
                    Polyline pl1 = new Polyline() { Closed = false, LayerId = lt["H标注"] };
                    pl1.AddVertexAt(0, startPoint.C2D(), 0, 0, 0);
                    pl1.AddVertexAt(1, lstP[i].C2D(), 0, 0, 0);
                    //pl1.AddVertexAt(2, startPoint.C2D(valueX, valueX * 0.5), 0, 0, 0);
                    ext = ext.Add(new Extents2d(pl1.Bounds.Value.MinPoint.C2D(), pl1.Bounds.Value.MaxPoint.C2D()));
                    res.Add(pl1);
                }

                foreach (DBObject drawitem in res)
                {
                    recorder.AppendEntity((Entity)drawitem);
                    tr.AddNewlyCreatedDBObject(drawitem, true);
                }

                tr.Commit();
            }
        }
        /// <summary>
        /// 焊接面
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="center">圆心</param>
        /// <param name="radius">原半径</param>
        /// <param name="scale">比例</param>
        /// <param name="replaceDim">替换焊接面的标注文字</param>
        public static void CreateFaceWeld(Database db, ref Extents2d ext, Point3d center,
            double radius, double scale, string replaceDim,string layer= "H粗线")
        {
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 焊接面夹角
                double angle = 10;

                // 焊接面厚度
                double thick = 0.5*scale;
                // 计算焊接面在圆上两点
                Polyline pl1 = new Polyline() { Closed = false, LayerId = lt[layer] };
                double offsetX = radius * Math.Sin(GeTools.DegreeToRadian(angle / 2));
                double offsetY = radius * Math.Cos(GeTools.DegreeToRadian(angle / 2));
                Point3d p1, p2, p3, p4;
                p1 = center.C3D(-offsetX, -offsetY);
                p2 = center.C3D(offsetX, -offsetY);
                p3 = p1.C3D(0, thick);
                p4 = p2.C3D(0, thick);

                Solid sod1 = new Solid(p3, p1, center.C3D(0, -radius + thick), center.C3D(0, -radius)) { LayerId = lt[layer] };
                ext = ext.Add(new Extents2d(sod1.Bounds.Value.MinPoint.C2D(), sod1.Bounds.Value.MaxPoint.C2D()));
                res.Add(sod1);

                Solid sod2 = new Solid(center.C3D(0, -radius), center.C3D(0, -radius + thick), p2, p4) { LayerId = lt[layer] };
                ext = ext.Add(new Extents2d(sod2.Bounds.Value.MinPoint.C2D(), sod2.Bounds.Value.MaxPoint.C2D()));
                res.Add(sod2);

                RotatedDimension dim = DimPloter.AddRotDim(db, ref ext, p1, p2, p1.C3D(0, 2 * scale), (int)scale, 0, Unit.Millimeter, Unit.Centimeter, replaceDim);
                //res.Add(dim);

                foreach (DBObject drawitem in res)
                {
                    recorder.AppendEntity((Entity)drawitem);
                    tr.AddNewlyCreatedDBObject(drawitem, true);
                }
                tr.Commit();
                //ext = ext.Add(new Extents2d(dim.Bounds.Value.MinPoint.C2D(), dim.Bounds.Value.MaxPoint.C2D()));

            }
        }

        /// <summary>
        /// 带箭头的直线
        /// </summary>
        /// <param name="db"></param>
        /// <param name="StartpPint"></param>
        /// <param name="lineLength"></param>
        /// <param name="scale"></param>
        /// <param name="Rotation"></param>
        public static DBObjectCollection ArrowLine(Database db, ref Extents2d ext,
             ArrowDirection eDir, Point3d StartpPint, double lineLength, double scale = 80, double Rotation = 0)
        {
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 绘制箭头
                Point2d pt1, pt2, pt3, pt4, pt5;
                switch (eDir)
                {
                    case ArrowDirection.North:  //上
                        {
                            // 直线
                            pt1 = StartpPint.C2D();
                            pt2 = StartpPint.C2D(0, lineLength - 2 * scale);

                            Polyline Line = new Polyline() { Closed = false, LayerId = lt["H粗线"] };
                            Line.AddVertexAt(0, pt1, 0, 0, 0);
                            Line.AddVertexAt(1, pt2, 0, 0, 0);
                            ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                            res.Add(Line);

                            // 箭头
                            pt3 = pt2.C2D(0, 2 * scale);
                            pt4 = pt2.C2D(0.5 * scale, 2 * scale);
                            pt5 = pt2.C2D(-0.5 * scale, 2 * scale);

                            Solid s1 = new Solid(pt3.C3D(), pt4.C3D(), pt5.C3D()) { LayerId = lt["H粗线"] };
                            ext = ext.Add(new Extents2d(s1.Bounds.Value.MinPoint.C2D(), s1.Bounds.Value.MaxPoint.C2D()));
                            res.Add(s1);
                            break;
                        }
                    case ArrowDirection.South:  //下
                        {
                            // 直线
                            pt1 = StartpPint.C2D();
                            pt2 = StartpPint.C2D(0, -lineLength + 2 * scale);

                            Polyline Line = new Polyline() { Closed = false, LayerId = lt["H粗线"] };
                            Line.AddVertexAt(0, pt1, 0, 0, 0);
                            Line.AddVertexAt(1, pt2, 0, 0, 0);
                            ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                            res.Add(Line);

                            // 箭头
                            pt3 = pt2.C2D(0, -2 * scale);
                            pt4 = pt2.C2D(0.5 * scale, -2 * scale);
                            pt5 = pt2.C2D(-0.5 * scale, -2 * scale);

                            Solid s1 = new Solid(pt3.C3D(), pt4.C3D(), pt5.C3D()) { LayerId = lt["H粗线"] };
                            ext = ext.Add(new Extents2d(s1.Bounds.Value.MinPoint.C2D(), s1.Bounds.Value.MaxPoint.C2D()));
                            res.Add(s1);

                            break;
                        }
                    case ArrowDirection.West:  // 左
                        {
                            // 直线
                            pt1 = StartpPint.C2D();
                            pt2 = StartpPint.C2D(-lineLength + 2 * scale);

                            Polyline Line = new Polyline() { Closed = false, LayerId = lt["H粗线"] };
                            Line.AddVertexAt(0, pt1, 0, 0, 0);
                            Line.AddVertexAt(1, pt2, 0, 0, 0);
                            ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                            res.Add(Line);

                            // 箭头
                            pt3 = pt2.C2D(-2 * scale);
                            pt4 = pt2.C2D(0, -0.5 * scale);
                            pt5 = pt2.C2D(0, 0.5 * scale);

                            Solid s1 = new Solid(pt3.C3D(), pt4.C3D(), pt5.C3D()) { LayerId = lt["H粗线"] };
                            ext = ext.Add(new Extents2d(s1.Bounds.Value.MinPoint.C2D(), s1.Bounds.Value.MaxPoint.C2D()));
                            res.Add(s1);
                            break;
                        }
                    case ArrowDirection.East:  //右
                        {
                            // 直线
                            pt1 = StartpPint.C2D();
                            pt2 = StartpPint.C2D(lineLength - 2 * scale);

                            Polyline Line = new Polyline() { Closed = false, LayerId = lt["H粗线"] };
                            Line.AddVertexAt(0, pt1, 0, 0, 0);
                            Line.AddVertexAt(1, pt2, 0, 0, 0);
                            ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                            res.Add(Line);

                            // 箭头
                            pt3 = pt2.C2D(2 * scale);
                            pt4 = pt2.C2D(0, 0.5 * scale);
                            pt5 = pt2.C2D(0, -0.5 * scale);

                            Solid s1 = new Solid(pt3.C3D(), pt4.C3D(), pt5.C3D()) { LayerId = lt["H粗线"] };
                            ext = ext.Add(new Extents2d(s1.Bounds.Value.MinPoint.C2D(), s1.Bounds.Value.MaxPoint.C2D()));
                            res.Add(s1);
                            break;
                        }
                }
                var TX1 = Matrix3d.Rotation(GeTools.DegreeToRadian(Rotation), Vector3d.ZAxis, StartpPint.C3D());

                foreach (DBObject drawitem in res)
                {
                    recorder.AppendEntity((Entity)drawitem);
                    tr.AddNewlyCreatedDBObject(drawitem, true);
                    Entity pr = (Entity)drawitem;
                    pr.TransformBy(TX1);
                }
                tr.Commit();
            }

            return res;
        }
        public static DBObjectCollection ArrowLineAndText(Database db, ref Extents2d ext,
       ArrowDirection eDir, Point3d StartpPint, double lineLength, double scale = 80, double Rotation = 0, string text = "", eTxtLocation eLocation = eTxtLocation.E_TOP, string tstyle = "SRB.HT",string layer="H标注")
        {
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 绘制箭头
                Point2d pt1 = new Point2d(), pt2 = new Point2d(), pt3, pt4, pt5;
                switch (eDir)
                {
                    case ArrowDirection.North:  //上
                        {
                            // 直线
                            pt1 = StartpPint.C2D();
                            pt2 = StartpPint.C2D(0, lineLength - 2 * scale);

                            Polyline Line = new Polyline() { Closed = false, LayerId = lt[layer] };
                            Line.AddVertexAt(0, pt1, 0, 0, 0);
                            Line.AddVertexAt(1, pt2, 0, 0, 0);
                            ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                            res.Add(Line);

                            // 箭头
                            pt3 = pt2.C2D(0, 2 * scale);
                            pt4 = pt2.C2D(0.5 * scale, 2 * scale);
                            pt5 = pt2.C2D(-0.5 * scale, 2 * scale);

                            Solid s1 = new Solid(pt3.C3D(), pt4.C3D(), pt5.C3D()) { LayerId = lt[layer] };
                            ext = ext.Add(new Extents2d(s1.Bounds.Value.MinPoint.C2D(), s1.Bounds.Value.MaxPoint.C2D()));
                            res.Add(s1);
                            break;
                        }
                    case ArrowDirection.South:  //下
                        {
                            // 直线
                            pt1 = StartpPint.C2D();
                            pt2 = StartpPint.C2D(0, -lineLength + 2 * scale);

                            Polyline Line = new Polyline() { Closed = false, LayerId = lt[layer] };
                            Line.AddVertexAt(0, pt1, 0, 0, 0);
                            Line.AddVertexAt(1, pt2, 0, 0, 0);
                            ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                            res.Add(Line);

                            // 箭头
                            pt3 = pt2.C2D(0, -2 * scale);
                            pt4 = pt2.C2D(0.5 * scale, -2 * scale);
                            pt5 = pt2.C2D(-0.5 * scale, -2 * scale);

                            Solid s1 = new Solid(pt3.C3D(), pt4.C3D(), pt5.C3D()) { LayerId = lt[layer] };
                            ext = ext.Add(new Extents2d(s1.Bounds.Value.MinPoint.C2D(), s1.Bounds.Value.MaxPoint.C2D()));
                            res.Add(s1);

                            break;
                        }
                    case ArrowDirection.West:  // 左
                        {
                            // 直线
                            pt1 = StartpPint.C2D();
                            pt2 = StartpPint.C2D(-lineLength + 2 * scale);

                            Polyline Line = new Polyline() { Closed = false, LayerId = lt[layer] };
                            Line.AddVertexAt(0, pt1, 0, 0, 0);
                            Line.AddVertexAt(1, pt2, 0, 0, 0);
                            ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                            res.Add(Line);

                            // 箭头
                            pt3 = pt2.C2D(-2 * scale);
                            pt4 = pt2.C2D(0, -0.5 * scale);
                            pt5 = pt2.C2D(0, 0.5 * scale);

                            Solid s1 = new Solid(pt3.C3D(), pt4.C3D(), pt5.C3D()) { LayerId = lt[layer] };
                            ext = ext.Add(new Extents2d(s1.Bounds.Value.MinPoint.C2D(), s1.Bounds.Value.MaxPoint.C2D()));
                            res.Add(s1);
                            break;
                        }
                    case ArrowDirection.East:  //右
                        {
                            // 直线
                            pt1 = StartpPint.C2D();
                            pt2 = StartpPint.C2D(lineLength - 2 * scale);

                            Polyline Line = new Polyline() { Closed = false, LayerId = lt[layer] };
                            Line.AddVertexAt(0, pt1, 0, 0, 0);
                            Line.AddVertexAt(1, pt2, 0, 0, 0);
                            ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                            res.Add(Line);

                            // 箭头
                            pt3 = pt2.C2D(2 * scale);
                            pt4 = pt2.C2D(0, 0.5 * scale);
                            pt5 = pt2.C2D(0, -0.5 * scale);

                            Solid s1 = new Solid(pt3.C3D(), pt4.C3D(), pt5.C3D()) { LayerId = lt[layer] };
                            ext = ext.Add(new Extents2d(s1.Bounds.Value.MinPoint.C2D(), s1.Bounds.Value.MaxPoint.C2D()));
                            res.Add(s1);
                            break;
                        }
                }
                var TX1 = Matrix3d.Rotation(GeTools.DegreeToRadian(Rotation), Vector3d.ZAxis, StartpPint.C3D());
                if (!string.IsNullOrWhiteSpace(text))
                {
                    Point3d Point = GeTools.MidPoint(pt1.C3D(), pt2.C3D());
                    MText Mtext = new MText();
                    Mtext.TextHeight = 2.5 * scale;
                    Mtext.Contents = text;
                    Mtext.TextStyleId = st[tstyle];
                    //Mtext.Width = 200 * _scale;
                    //Mtext.Rotation = GeTools.DegreeToRadian(Rotation);
                    //res.Add(Mtext);
                    switch (eLocation)
                    {
                        case eTxtLocation.E_LEFT:
                            {
                                Mtext.Location = Point.C3D(-Mtext.ActualHeight-1 * scale,1 * scale);
                                Mtext.Attachment = AttachmentPoint.TopCenter;
                                break;
                            }
                        case eTxtLocation.E_RIGHT:
                            {
                                Mtext.Location = Point.C3D(Mtext.ActualHeight+1*scale, 1 * scale);
                                Mtext.Attachment = AttachmentPoint.BottomCenter;
                                break;
                            }
                        case eTxtLocation.E_TOP:
                            {
                                Mtext.Location = Point.C3D(1 * scale, Mtext.ActualHeight+ 1 * scale);
                                Mtext.Attachment = AttachmentPoint.TopCenter;
                                break;
                            }
                        case eTxtLocation.E_BOTTOM:
                            {
                                Mtext.Location = Point.C3D(1 * scale, -Mtext.ActualHeight - 1 * scale);
                                Mtext.Attachment = AttachmentPoint.BottomCenter;
                                break;
                            }
                        default:
                            break;
                    }
                    //Mtext.TransformBy(TX1);
                    //recorder.AppendEntity(Mtext);
                    //tr.AddNewlyCreatedDBObject(Mtext, true);
                    ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.C2D(), Mtext.Bounds.Value.MaxPoint.C2D()));
                    res.Add(Mtext);
                }



                foreach (DBObject drawitem in res)
                {
                    recorder.AppendEntity((Entity)drawitem);
                    tr.AddNewlyCreatedDBObject(drawitem, true);
                    Entity pr = (Entity)drawitem;
                    pr.TransformBy(TX1);
                }
                tr.Commit();
            }

            return res;
        }

        /// <summary>
        /// 直径标注
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="startPoint"></param>
        /// <param name="arrow">箭头方向</param>
        /// <param name="diameter">直径</param>
        /// <param name="diameter">标记字符串</param>
        /// <param name="scale">比例</param>
        public static void CreateDiameterMark(Database db, ref Extents2d ext, Point3d startPoint,
            eArrow arrow, double diameter, string T1, double scale, double rotation,string layer= "H标注")
        {
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 半径
                double radius = diameter / 2;

                Point2d pt1, pt2, pt3, pt4, pt5;
                double offsetX = 0;
                double offsetY = 0;
                Polyline pl3 = new Polyline() { Closed = false, LayerId = lt[layer] };
                pl3.AddVertexAt(0, startPoint.C2D(), 0, 0, 0);
                pl3.AddVertexAt(1, startPoint.C2D(radius * 2), 0, 0, 0);
                ext = ext.Add(new Extents2d(pl3.Bounds.Value.MinPoint.C2D(), pl3.Bounds.Value.MaxPoint.C2D()));

                // 左边箭头
                double multi = scale;
                pt3 = startPoint.C2D(2 * multi, 0.5 * multi);
                pt4 = startPoint.C2D(2 * multi, -0.5 * multi);

                Solid s1 = new Solid(startPoint.C3D(), pt3.C3D(), pt4.C3D()) { LayerId = lt[layer] };
                ext = ext.Add(new Extents2d(s1.Bounds.Value.MinPoint.C2D(), s1.Bounds.Value.MaxPoint.C2D()));

                // 右边箭头
                pt3 = startPoint.C2D(radius * 2 - 2 * multi, 0.5 * multi);
                pt4 = startPoint.C2D(radius * 2 - 2 * multi, -0.5 * multi);

                Solid s2 = new Solid(startPoint.C3D(radius * 2), pt3.C3D(), pt4.C3D()) { LayerId = lt[layer] };
                ext = ext.Add(new Extents2d(s2.Bounds.Value.MinPoint.C2D(), s2.Bounds.Value.MaxPoint.C2D()));

                DBObjectCollection res2 = TextPloter.PlotText(db, ref ext, TextPloter.eTxtLocation.E_TOP, startPoint.C2D(radius, 1 * scale),
                    T1, scale, Extensions.curFont, GeTools.DegreeToRadian(rotation));
                switch (arrow)
                {
                    case eArrow.E_ARROW_NULL:           // 无箭头
                        {
                            res.Add(pl3);
                            break;
                        }

                    case eArrow.E_ARROW_LEFT_ARROW:     // 左侧箭头
                        {
                            res.Add(pl3);
                            res.Add(s1);
                            break;
                        }
                    case eArrow.E_ARROW_RIGHT_ARROW:    // 右侧箭头
                        {
                            res.Add(pl3);
                            res.Add(s2);
                            break;
                        }
                    case eArrow.E_ARROW_DOUBLE_SIDE_ARROW:  // 双侧箭头
                        {
                            res.Add(pl3);
                            res.Add(s1);
                            res.Add(s2);
                            break;
                        }
                    default:
                        break;
                }
                // 旋转
                var TX1 = Matrix3d.Rotation(GeTools.DegreeToRadian(rotation), Vector3d.ZAxis, startPoint.C3D(radius));
                foreach (DBObject item in res)
                {
                    Entity pr = (Entity)item;
                    pr.TransformBy(TX1);
                }
                //foreach (DBObject item in res2)
                //{
                //    Entity pr = (Entity)item;
                //    pr.TransformBy(TX1);
                //}
                foreach (DBObject drawitem in res)
                {
                    recorder.AppendEntity((Entity)drawitem);
                    tr.AddNewlyCreatedDBObject(drawitem, true);
                }

                tr.Commit();
            }
        }
        #endregion
    }
}
